package huoye.shops.huoye.mine.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.codeideology.android.utils.LoginUtils;
import com.codeideology.android.widget.ios.dialog.NormalDialog;
import com.codeideology.android.widget.ios.dialog.listener.OnBtnClickL;

import java.math.BigDecimal;

import butterknife.BindView;
import butterknife.OnClick;
import huoye.shops.MainActivity;
import huoye.shops.R;
import huoye.shops.base.APIUrls;
import huoye.shops.base.BaseFragment;
import huoye.shops.base.UserManager;
import huoye.shops.huoye.mine.activity.DividendActivity;
import huoye.shops.huoye.mine.activity.ManageAccountActivity;
import huoye.shops.huoye.mine.activity.ServiceSettingActivity;
import huoye.shops.huoye.mine.activity.ShopMessageActivity;
import huoye.shops.huoye.mine.activity.WithdrawActivity;
import huoye.shops.huoye.mine.presenter.MinePresenter;
import huoye.shops.huoye.mine.view.IMineView;
import huoye.shops.huoye.operation.activity.DiscountCancelActivity;
import huoye.shops.huoye.user.activity.LoginActivity;
import huoye.shops.huoye.user.model.User;
import huoye.shops.utils.StockUtils;
import huoye.shops.widget.RoundImageView;

/**
 * A simple {@link Fragment} subclass.
 */
public class MineFragment extends BaseFragment implements IMineView{


    @BindView(R.id.mine_rv_head)
    RoundImageView rvHead;
    @BindView(R.id.mine_tv_versions)
    TextView tvVersions;
    @BindView(R.id.mine_tv_shop_name)
    TextView tvShopName;
    @BindView(R.id.mine_tv_balance)
    TextView tvBalance;
    private MinePresenter mMinePresenter;


    @OnClick(R.id.mine_iv_more)
    void onMoreClick(){
        outAccound();
    }
    @OnClick(R.id.mine_btn_withdraw)
    void onWithdrawClick(){
        startActivity(WithdrawActivity.class);
    }
    @OnClick(R.id.mine_ll_balance)
    void onBalanceClick(){
       // makeToast("3");
    }

    @OnClick(R.id.mine_ll_service_setting)
    void onServiceSettingClick(){
       startActivity(ServiceSettingActivity.class);
    }

    @BindView(R.id.mine_ll_manage_user)
    LinearLayout llManageUser;

    @BindView(R.id.mine_ll_dividend)
    LinearLayout llDividend;



    @OnClick(R.id.mine_ll_manage_user)
    void onManageUserClick(){
        startActivity(ManageAccountActivity.class);
    }
    @OnClick(R.id.mine_ll_message_shop)
    void onMessageShopClick(){
        startActivity(ShopMessageActivity.class);
    }

    @OnClick(R.id.mine_ll_dividend)
    void onDividendClick(){
        startActivity(DividendActivity.class);
    }


    @Override
    public int getContentViewId() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initView(View view, @Nullable Bundle bundle) {


        mMinePresenter = new MinePresenter();
        mMinePresenter.addTaskListener(this);
        mMinePresenter.onStart();
        mMinePresenter.getUser();


        boolean powerManage = UserManager.getInstance().getPowerManage();
        if(powerManage){
            llManageUser.setVisibility(View.VISIBLE);
            llDividend.setVisibility(View.VISIBLE);
        }else {
            llManageUser.setVisibility(View.GONE);
            llDividend.setVisibility(View.GONE);

        }
    }


    @Override
    public void onResume() {
        super.onResume();
        if(mMinePresenter != null){
            mMinePresenter.getUser();
        }
    }

    //退出账户
    private void outAccound(){
        final NormalDialog dialog = new NormalDialog(getActivity());
        dialog.isTitleShow(false);
        dialog.content("确定退出当前账号?")//
                .style(NormalDialog.STYLE_TWO)//
                .titleTextSize(23)//
                .widthScale(0.75f)
                .show();

        dialog.setOnBtnClickL(
                new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        dialog.dismiss();
                    }
                },
                new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {

                        UserManager.getInstance().logout();
                        dialog.dismiss();
                        //LoginUtils.startActivity(getActivity(), MainActivity.class);
                        Intent intent = new Intent(getActivity(), LoginActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
                        startActivity(intent);

                    }
                });
    }

    @Override
    public void getUserData(User user) {


        //测试数据
        if(user.isBasicsVersion()){
            tvVersions.setText("基础版");
            tvVersions.setSelected(false);
            tvVersions.setBackgroundResource(R.mipmap.img_versions_basics);
        }else {
            tvVersions.setText("豪华版");
            tvVersions.setSelected(true);
            tvVersions.setBackgroundResource(R.mipmap.img_versions_deluxe);
        }



        Glide.with(this)
                .load(APIUrls.COMMON_CONFIG.TYPE_COMMON_ALIYUN+user.getUserImg())
                .dontAnimate()
                .placeholder(R.mipmap.ic_error)
                .error(R.mipmap.ic_error)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .into(rvHead);
        UserManager.getInstance().saveShopLogo(user.getUserImg());

        BigDecimal moeny = StockUtils.moenyStyle(user.getBalance());
        tvBalance.setText(moeny+"");
        tvShopName.setText(user.getShopName());

    }




}
